<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use DB;

class HomeuserController extends Controller
{
    //index
    public function index(Request $request)
    {
        $users = DB::table('home_users')
                -> where('name','like','%'.$request -> input('keywords').'%')
                -> paginate($request -> input('num',10));
               
//        dd($users);
        return view('admin.homeuser.index',['users' => $users,'request' => $request -> all()]);
    }
    
    //ajaxUpdate
   public function ajaxUpdate(Request $request)
    {
//        return 111;
        $id = $request -> input('id');
        
       $data =  DB::table('home_users') -> select('status') -> where('id',$id) -> first();
//       dd($data);
        //判断
       if($data -> status == 0)
       {
           $res = DB::table('home_users') -> where('id',$id) -> update(['status' => 1]);
           if($res)
           {
                return response() -> json(1);
           } else
           {
                 //2 表示失败
               return response() -> json(2);
           }
          
       } else
       {
          $res =  DB::table('home_users') -> where('id',$id) -> update(['status' => 0]);
           if($res)
           {
                return response() -> json(0);
           } else
           {
               //2 表示失败
               return response() -> json(2);
           }
       } 
    }
    
}
